package com.ztjava.common.validator;

import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.ztjava.common.service.PushService;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseValidator;
import com.ztjava.core.tool.ViewTool;
import com.ztjava.core.utiliy.RequestUtil;
import com.ztjava.ext.auth.annotation.UserController;

public class PushValidator extends BaseValidator{
	private static PushService pushService = Duang.duang(PushService.class);

	@Override
	protected void validate(Controller c) {
		setShortCircuit(true);

		String portal = c.getClass().getAnnotation(UserController.class).portal().toUpperCase();
		
		validateRequired("auth_type", Constant.MESSAGE, "缺少参数！");
		String mode = ViewTool.param("AUTH_"+portal+"_REGISTER_TYPE");
		if(mode.equals("PHONE")){
			validateRequired("to", Constant.MESSAGE, "请填写手机号！");
			validateMobilePattern("to", Constant.MESSAGE, "手机号格式不正确！");
		}else if (mode.equals("EMAIL")){
			validateRequired("to", Constant.MESSAGE, "请填写邮箱地址！");
			validateEmail("to", Constant.MESSAGE, "邮箱地址格式不正确！");
		}else {
			addError(Constant.MESSAGE, "无法发送信息，请联系管理员！");
		}
		
		if(!pushService.checkMessageTime(portal, c.getPara("to"),60)){
			addError(Constant.MESSAGE, "距离上次发送不足1分钟，请稍候再试！");
		}
		
		if(!pushService.checkMessageCount(portal, c.getPara("to"), 24, 5)){
			addError(Constant.MESSAGE, "发送次数过多！");
		}
		if(ViewTool.param("COMMON_PUSH_IP_CHECK_"+portal,"ON").equals("ON")){
			if(!pushService.checkMessageIpCount(portal,RequestUtil.getIp(c.getRequest()), 24, 10)){
				addError(Constant.MESSAGE, "同IP发送次数过多！");
			}
			
		}
	}

}
